package service

import (
	dom "a-common/domain"
	"a-common/utils/excel"
	"a-system/dao"
	"a-system/domain/entity"
	"sync"

	"github.com/xuri/excelize/v2"
)

var (
	sysPostServiceTemp *SysPostService
	sysPostDao         = dao.NewSysPostDao()
	sysPostServiceOnce sync.Once
)

type SysPostService struct{}

func NewSysPostService() *SysPostService {
	sysPostServiceOnce.Do(func() {
		sysPostServiceTemp = &SysPostService{}
	})
	return sysPostServiceTemp
}

// List 分页
func (m *SysPostService) List(args *entity.SysPostDto) (dom.PageVo, error) {
	return sysPostDao.List(args, false)
}

// Get 查
func (m *SysPostService) Get(id string) (entity.SysPostVo, error) {
	return sysPostDao.Get(id)
}

// Del 删
func (m *SysPostService) Del(ids string) (int64, error) {
	return sysPostDao.Del(ids)
}

// Add 增
func (m *SysPostService) Add(args *entity.SysPostDto) (int64, error) {
	return sysPostDao.Add(args)
}

// Edit 改
func (m *SysPostService) Edit(args *entity.SysPostDto) (int64, error) {
	return sysPostDao.Edit(args)
}

// Export 导出
func (m *SysPostService) Export(args *entity.SysPostDto, title string) (*excelize.File, error) {
	pageVo, err := sysPostDao.List(args, true)
	if err != nil {
		return nil, err
	}
	return excel.NormalDynamicExport(pageVo.Rows, "Sheet1", title, "", true, false, map[string]string{})
}
